﻿using System;
using System.Data;
using System.Linq;
using BO.News;
using GUI.Extensions;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using System.Text;
using Utilities.Entities;

namespace GUI.Ajax.News
{
    public partial class ZonePreview : System.Web.UI.Page
    {
        private const int ImageWidth = 345;
        private const int ImageHeight = 230;

        //tieu chi : lay 3 tin noi bat chuyen muc va 2 tin moi nhat
        private const int TopHighlight = 3;
        private const int TopOthers = 2;

        private const string TopItemFormat = "{3}<br /><h3><a href=\"{0}\" title=\"{2}\">{1}</a></h3>";
        private const string LiFormat = "<li><h5><a href=\"{0}\" title=\"{2}\">{1}</a></h5><span>{3}</span></li>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var catMeta = RequestExtensions.Category;

                if (catMeta != null)
                {
                    var dt = NewsBO.GetInstance().GetNewsByPosition(
                    top: TopHighlight,
                    typeId: DefinedConstants.News.TypeIDs.NoiBatChuyenMuc,
                    zoneId: catMeta.Id);
                    var sb = new StringBuilder();

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        for (var i = 0; i < dt.Rows.Count; i++)
                        {
                            var r = dt.Rows[i];
                            if (i == 0)
                            {
                                var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight);

                                ltrHighlightItem.Text = string.Format(
                                        TopItemFormat,
                                        r[SqlConstants.NewsPublish.Url],
                                        r[SqlConstants.NewsPublish.Title],
                                        r[SqlConstants.NewsPublish.EncodedTitle],
                                        avatar
                                    );
                            }
                            else
                            {
                                sb.AppendFormat(LiFormat,
                                                r[SqlConstants.NewsPublish.Url],
                                                r[SqlConstants.NewsPublish.Title],
                                                r[SqlConstants.NewsPublish.EncodedTitle],
                                                PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("dd/MM/yyyy HH:mm"));
                            }
                        }
                    }

                    dt = NewsBO.GetInstance().GetNewsByPosition(top: TopOthers,
                                                  typeId: DefinedConstants.News.TypeIDs.TinTrangChuyenMuc, zoneId: catMeta.Id);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow r in dt.Rows)
                        {
                            sb.AppendFormat(LiFormat,
                                                r[SqlConstants.NewsPublish.Url],
                                                r[SqlConstants.NewsPublish.Title],
                                                r[SqlConstants.NewsPublish.EncodedTitle],
                                                PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("dd/MM/yyyy HH:mm"));
                        }
                    }

                    ltrOtherItem.Text = sb.ToString();

                    lstTags.Visible = LoadChildCategories(catMeta);
                }
            }
        }

        private bool LoadChildCategories(CategoryObject objCat)
        {
            if (objCat != null)
            {
                var allCategoriesMetaData = NewsBO.GetInstance().GetAllCategories();

                var query = from c in allCategoriesMetaData
                            where c.ParentId == objCat.Id
                            select c;

                var categoryObjects = query as CategoryObject[] ?? query.ToArray();
                if (categoryObjects.Any())
                {
                    const string format = "<li><a href=\"{0}\" title=\"{1}\">{1}</a></li>";
                    var sb = new StringBuilder();
                    foreach (var c in categoryObjects)
                    {
                        sb.AppendFormat(format, c.Url, c.Name);

                    }

                    ltrChildCat.Text = sb.ToString();
                    return true;
                }
            }
            return false;
        }
    }
}